#!/bin/bash
set -ex

if [[ $DRONE_TAG != v2* ]]; then
  echo "Tag ${DRONE_TAG} is not a Rancher tag, not dispatching workflow for rancher/rancher"
  exit 0
fi

REPO="https://api.github.com/repos/rancher/rancher/actions/workflows/replace-env-value.yml/dispatches"

# Only use first two parts, delimited by dot
# v2.6.5 becomes v2.6, end result: release/v2.6
ACTION_TARGET_BRANCH="release/$(echo $DRONE_TAG | cut -f1,2 -d".")"

echo "Dispatching to branch ${ACTION_TARGET_BRANCH} with input envvalue=${DRONE_TAG}"

# send dispatch event to workflow
curl -XPOST -u "${PAT_USERNAME}:${PAT_TOKEN}" \
        -H "Accept: application/vnd.github.v3+json"  \
        -H "Content-Type: application/json" $REPO \
        --data '{"ref":"'"${ACTION_TARGET_BRANCH}"'","inputs":{"filepath":"package/Dockerfile","envvar":"CATTLE_DASHBOARD_UI_VERSION","envvalue":"'"${DRONE_TAG}"'"}}'
